<?php

namespace app\controller\api\piles;

use think\App;
use app\controller\api\Base;
use app\common\repositories\piles\PilesBasicRepository;

class PilesBasic extends Base
{
    protected $repository;

    public function __construct(App $app, PilesBasicRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 获取充电桩列表
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 19:29
     */
    public function pilesList()
    {
        $where = $this->request->param([
            'keywords' => '',
            'longitude' => '',
            'latitude' => '',
            'piles_type' => '',
        ]);
        $where['is_show'] = 1;
        [$page, $limit] = $this->getPage();
        $res = $this->repository->getApiList($where, $page, $limit, $this->request->propertyId);
        return app('api_return')->success($res);
    }

    /**
     *  获取充电桩详情
     * @param int $id
     * @return mixed
     *
     * @author Amos
     * 2024-06-12 19:51
     */
    public function detail(int $id)
    {
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }
        $info = $this->repository->getApiDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }


    /**
     *  开启充电
     * @param int $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     *
     * @author Amos
     * 2024-06-12 20:13
     */
    public function startCharge(int $id)
    {
        $where = $this->request->param([
            'port_no' => '',
        ]);
        if (!$this->repository->exists($id)) {
            return $this->error('数据不存在');
        }

        $info = $this->repository->getApiDetail($id);
        if (!$info) {
            return app('api_return')->error('数据不存在');
        }
        return app('api_return')->success($info);
    }

}